<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    
    <title>后台运行Linux程序的方法 | xilixili.net</title>
    <meta name="renderer" content="webkit">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <meta name="description" content="一般远程登陆LINUX，在网络断开及注销等情况下，进程会接收到HUP信号，从而导致进程被关闭。解决方案一个是让进程忽略该信号，另一个是让进程运行在新 会话中，使其不属于此终端的子进程。下面是几种实现进程后台运行的方法：">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="后台运行Linux程序的方法 | xilixili.net">
    <meta name="twitter:description" content="一般远程登陆LINUX，在网络断开及注销等情况下，进程会接收到HUP信号，从而导致进程被关闭。解决方案一个是让进程忽略该信号，另一个是让进程运行在新 会话中，使其不属于此终端的子进程。下面是几种实现进程后台运行的方法：">

    <meta property="og:type" content="article">
    <meta property="og:title" content="后台运行Linux程序的方法 | xilixili.net">
    <meta property="og:description" content="一般远程登陆LINUX，在网络断开及注销等情况下，进程会接收到HUP信号，从而导致进程被关闭。解决方案一个是让进程忽略该信号，另一个是让进程运行在新 会话中，使其不属于此终端的子进程。下面是几种实现进程后台运行的方法：">

    
    <meta name="author" content="rangerlee">
    
    <link rel="stylesheet" href="/css/vno.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">

    
    <link rel="icon" href="/images/avatar-small.png">
    

    <meta name="generator" content="hexo"/>
    
    <link rel="alternate" type="application/rss+xml" title="xilixili.net" href="/atom.xml">
    

    <link rel="canonical" href="xilixili.net/2016/04/27/run-linux-program-in-background/"/>

                 
</head>

<body class="home-template no-js">
    <script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/js/main.js"></script>
    <span class="mobile btn-mobile-menu">
        <i class="fa fa-list btn-mobile-menu__icon"></i>
        <i class="fa fa-angle-up btn-mobile-close__icon hidden"></i>
    </span>

    
<header class="panel-cover panel-cover--collapsed">
  <div class="panel-main">
    <div class="panel-main__inner panel-inverted">
    <div class="panel-main__content">

        <h1 class="panel-cover__title panel-title"><a href="/" title="访问首页 xilixili.net">xilixili.net</a></h1>
        
        <span class="panel-cover__subtitle panel-subtitle"><!-- 生活と技术の杂记 --></span>
        
        <hr class="panel-cover__divider">
        
        <p class="panel-cover__description">生活と技术の杂记</p>
        
	<hr class="panel-cover__divider panel-cover__divider--secondary">

        <div class="navigation-wrapper">
          <div>
          <nav class="cover-navigation cover-navigation--primary">
            <ul class="navigation">
              <li class="navigation__item"><a href="/#blog" title="访问博客" class="blog-button">博客</a></li>
            
            </ul>
          </nav>
          </div>
          <div>
          <nav class="cover-navigation navigation--social">
  <ul class="navigation">

  <!-- Weibo-->
  

  <!-- Github -->
  
  <li class="navigation__item">
    <a href="https://github.com/rangerlee" title="查看我的GitHub主页" target="_blank">
      <i class="social fa fa-github"></i>
      <span class="label">Github</span>
    </a>
  </li>


<!-- Stack Overflow -->
        

  <!-- Google Plus -->
  

<!-- Facebook -->

  
<!-- Twitter -->

  

  <li class="navigation__item">
    <a href="/atom.xml" title="RSS" target="_blank">
      <i class="social fa fa-rss"></i>
      <span class="label">RSS</span>
    </a>
  </li>


  <li class="navigation__item">
    <a href="mailto:http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=RzUmKSAiNSsiIgchKD8qJi4raSQoKg" title="邮件联系我" target="_blank">
      <i class="social fa fa-envelope"></i>
      <span class="label">Email</span>
    </a>
  </li>


  </ul>
</nav>

          </div>
        </div>

      </div>

    </div>

    <div class="panel-cover--overlay cover-alpha"></div>
  </div> 
</header>
<script type="text/javascript">
var side = document.getElementsByClassName("panel-cover")[0];
function checkWebp() {
	try{
		return(document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0);
	}catch(err) {
		return false;
	}
};

if(checkWebp()) {
	side.style.backgroundImage = "url('/images/background-cover.jpg.webp')";
} else {
	side.style.backgroundImage = "url('/images/background-cover.jpg')";
}
</script>


    <div class="content-wrapper">
        <div class="content-wrapper__inner">
            <article class="post-container post-container--single">

  <header class="post-header">
    <div class="post-meta">
      <time datetime="2016-04-27T01:58:51.000Z" class="post-list__meta--date date">2016-04-27</time> &#8226; <span class="post-meta__tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
 </span>
      <span class="page-pv">
       阅读 <span id="busuanzi_value_page_pv"><i class="fa fa-spinner fa-spin"></i></span>
      </span> 
   
    </div>
    <h1 class="post-title">后台运行Linux程序的方法</h1>
  </header>

  <section class="post">
    <p>一般远程登陆LINUX，在网络断开及注销等情况下，进程会接收到HUP信号，从而导致进程被关闭。解决方案一个是让进程忽略该信号，另一个是让进程运行在新 会话中，使其不属于此终端的子进程。<br>下面是几种实现进程后台运行的方法：<br><a id="more"></a></p>
<h3 id="nohup"><a href="#nohup" class="headerlink" title="nohup"></a>nohup</h3><p>nohup是最简单的方法，该命令用途就是让提交的命令忽略hangup信号。使用方法为在需要处理的命令前加nohup即可，标准输出和错误会被重定向到nohup.out文件，也可以更改默认的重定向文件名。一般我们可在结尾添加”&amp;”来将命令同时放入后台运行。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nohup  COMMAND  &amp;</span><br></pre></td></tr></table></figure>
<p>注意：需要同时按两次回车。</p>
<h3 id="setsid"><a href="#setsid" class="headerlink" title="setsid"></a>setsid</h3><p>如果运行的程序不属于接受HUP信号的终端子进程，同样将不会受到HUP影响，setsid即可实现该功能。使用方法为在提交的命令前加setsid即可，进程将运行在一个新的session中。<br>通过setsid运行的进程的父进程为1，即init进程，并不是类似nohup的前端进程ID。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">setsid  COMMAND</span><br></pre></td></tr></table></figure>
<h3 id="subshell"><a href="#subshell" class="headerlink" title="subshell"></a>subshell</h3><p>subshell有个小技巧，shell中将命令放在()中，将会让命令在shell的子进程中运行，当我们将&amp;也放入到()内之后，所提交的命令的父进程为1，即init进程。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">(COMMAND  &amp;)</span><br></pre></td></tr></table></figure>

  </section>

</article>

<section class="read-more">
           
    
               
            <div class="read-more-item">
                <span class="read-more-item-dim">最近的文章</span>
                <h2 class="post-list__post-title post-title"><a href="/2016/06/13/ffmpeg-decode-audio/" title="ffmpeg编解码音频相关问题">ffmpeg编解码音频相关问题</a></h2>
                <p class="excerpt">
                
                重拾FFMPEG，版本已经3.0了，和我很早使用的0.8版本的变化还是很大的。项目要实现音频转M4R格式（AAC编码）的功能，编码倒是问题不大，解码测试时还是遇到了好几个问题，记录一下。
编码数据丢失使用 avcodec_decode_audio4 解码出来的AVFrame在 avcodec_enc
                &hellip;
                </p>
                <div class="post-list__meta"><time datetime="2016-06-13T02:20:44.000Z" class="post-list__meta--date date">2016-06-13</time> &#8226; <span class="post-list__meta--tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
</span><a class="btn-border-small" href="/2016/06/13/ffmpeg-decode-audio/">继续阅读</a></div>
                           
            </div>
        
        
        
     
   
   
  
</section>



	<script>
	(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(bp, s);
	})();
	</script>



<script>
(function(){
var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?8612a7fa26d7ba4562be733ae33649b1":"https://jspassport.ssl.qhimg.com/11.0.1.js?8612a7fa26d7ba4562be733ae33649b1";
document.write('<script src="' + src + '" id="sozz"><\/script>');
})();
</script>



            <footer class="footer">
    <span class="footer__copyright">
        &copy; 2023 rangerlee - 本站点采用 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>
       
    </span>
    <span class="footer__copyright">
             - 本站基于开源 <a href="http://hexo.io">Hexo</a> 搭建，采用 <a href="https://github.com/rangerlee/hexo-theme-new-vno">hexo-theme-new-vno</a> 主题，修改自 <a href="https://github.com/monniya/hexo-theme-new-vno ">new-vno</a> 主题  <a href="http://www.beian.miit.gov.cn">皖ICP备16009457号</a>
         </span>
       
    
    
</footer>


        </div>
    </div>

     
    
    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?bab4f71524319c10819bcae280021624";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>



    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
    
</body>
</html>
